package com.peng.edu.controller;



import com.peng.base.exception.GuliException;
import com.peng.edu.client.VodClient;
import com.peng.edu.entity.Video;
import com.peng.edu.service.VideoService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-16
 */
@RestController
@RequestMapping("/edu/video")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public Result addVideo(@RequestBody Video video){
        videoService.save(video);
        return Result.ok();
    }


    //删除小节
    // TODO 后面这个方法需要完善，删除小节的时候，同时也要把视频删除
    @DeleteMapping("/deleteVideo/{id}")
    @Transactional
    public Result deleteVideo(@PathVariable String id){
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (videoSourceId!=null){
            Result r=vodClient.removeAliyunVideo(videoSourceId);
            if (!r.getSuccess())
                throw new GuliException(20001,"asd");
        }
        videoService.removeById(id);
        return Result.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public Result updateVideo(@RequestBody Video video){
        videoService.updateById(video);
        return Result.ok();
    }

    //根据小节id查询
    @GetMapping("/getVideoById/{videoId}")
    public Result getVideoById(@PathVariable String videoId){
        Video eduVideo = videoService.getById(videoId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("video",eduVideo);
        return Result.ok().data(map);
    }
}

